const wx = require('./common');
const db=uniCloud.database()
// 添加素材
async function add(data) {
	// 获取参数
	let group={
		group:data
	}
	let access_token = await wx.getToken()
	let Url =
		`https://api.weixin.qq.com/cgi-bin/groups/create?access_token=${access_token}`
	let result = await uniCloud.httpclient.request(Url, {
		method: 'POST',
		data:JSON.stringify(group),
		dataType: 'json'
	})
	// 添加到后台服务器
	return result
	
}
// 修改素材
async function edit(data) {
	// 获取参数
	let group={
		group:data
	}
	let access_token = await wx.getToken()
	let Url =
		`https://api.weixin.qq.com/cgi-bin/groups/update?access_token=${access_token}`
	let result = await uniCloud.httpclient.request(Url, {
		method: 'POST',
		data:JSON.stringify(group),
		dataType: 'json'
	})
	// 添加到后台服务器
	return result
}
// 获取单个素材
async function get(data) {

}
async function remove(data) {
	// 获取参数
	let access_token = await wx.getToken()
	let Url =
		`https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=${access_token}`
	let result = await uniCloud.httpclient.request(Url, {
		method: 'POST',
		data:JSON.stringify(data),
		dataType: 'json'
	})
	// 返回客户端
	return result
}
// 删除素材
async function deleted(data) {
	// 获取参数
	let group={
		group:data
	}
	let access_token = await wx.getToken()
	let Url =
		`https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=${access_token}`
	let result = await uniCloud.httpclient.request(Url, {
		method: 'POST',
		data:JSON.stringify(group),
		dataType: 'json'
	})
	// 添加到后台服务器
	return result
}

// 获取素材列表
async function getList(data) {
	let access_token = await wx.getToken()
	let Url =
		`https://api.weixin.qq.com/cgi-bin/groups/get?access_token=${access_token}`
	let listRes = await uniCloud.httpclient.request(Url, {
		method: 'POST',
		data: {},
		dataType: 'json'
	})
	if(listRes.status===200){
		return {
			data: listRes.data,
			status: 200
		}
	}else {
		return {
			msg: '服务器出错',
			data: false,
			status: 400
		}
	}
}



module.exports = {
	add,
	edit,
	get,
	deleted,
	getList,
	remove
}
